package com.yungu.swift.api.route.v1.driver;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yungu.swift.api.base.BaseApi;
import com.yungu.swift.api.base.gateway.GatewayHeader;
import com.yungu.swift.assets.driver.model.dto.DriverDto;
import com.yungu.swift.assets.driver.service.DriverService;
import com.yungu.swift.assets.passenger.model.dto.PassengerDto;
import com.yungu.swift.assets.passenger.service.PassengerService;
import com.yungu.swift.autoconfig.redis.RedisStringCacheService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.order.model.dto.OrderComplainDto;
import com.yungu.swift.order.model.dto.OrderDto;
import com.yungu.swift.order.model.param.ApiOrderListParam;
import com.yungu.swift.order.model.vo.JsonOrderListVo;
import com.yungu.swift.order.service.OrderComplainService;
import com.yungu.swift.order.service.OrderService;
import com.yungu.swift.system.sys.model.vo.SysDriverAccountVo;
import com.yungu.swift.system.sys.model.vo.SysPassengerAccountVo;
import com.yungu.swift.system.sys.service.SysAccountDriverService;
import com.yungu.swift.system.sys.service.SysAccountPassengerService;
import com.yungu.swift.utils.JsonUtils;
import com.yungu.swift.utils.MapUtils;
import com.yungu.swift.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>描述：</p>
 *
 * @author xuzhicheng
 * @Time 2016/12/12
 */
@Slf4j
@Controller
@RequestMapping(value = "/api/v1/driver")
@Api(value = "【司机】司机注销接口", tags = "司机注销操作")
public class DriCancelApi extends BaseApi {

    @Reference
    private PassengerService passengerService;
    @Reference
    private DriverService driverService;
    @Reference
    private SysAccountDriverService sysAccountDriverService;
    @Reference
    private OrderService orderService;
    @Reference
    private OrderComplainService orderComplainService;

    @Autowired
    private RedisStringCacheService redisStringCacheService;
    public static final String ACCESS_TOKEN_PREFFIX = "DRIVER_ACCESS_TOKEN_";
    public static final String REFRESH_TOKEN_PREFFIX = "DRIVER_REFRESH_TOKEN_";
    public static final String DRIVER_UUID = "DRIVER_UUID_";

    @RequestMapping(value = {"/cancel"}, method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @ApiOperation(value = "司机注销接口", notes = "司机注销", response = ResponseData.class)
    public ResponseData<Boolean> addComment(
            @ApiParam(name = "appid", value = "APP标识", required = true) @RequestParam String appid,
            @ApiParam(name = "uuid", value = "uuid", required = true) @RequestParam String uuid) {
        //获取当前用户钱包是否为0
        Map<String, Object> map = MapUtils.build();
        map.put("uuid", uuid);
        SysDriverAccountVo data = sysAccountDriverService.findAccountDriverVo(map).getData();
        if (data.getFrozenBalance().compareTo(new BigDecimal("0")) > 0 || data.getCanFrowardBalance().compareTo(new BigDecimal("0")) > 0) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "钱包余额大于0，不能注销");
        }
        //获取当前用户是否有进行中的订单
        ApiOrderListParam apiOrderListParam = new ApiOrderListParam();
        apiOrderListParam.setUserUuid(uuid);
        List<JsonOrderListVo> orderList = orderService.queryOrderDriDoing(apiOrderListParam).getData();
        if (orderList.size() > 0) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "你有正在进行中的订单，不能注销");
        }
        //获取当前用户是否有未支付的订单
        Map<String, Object> maps = MapUtils.build();
        maps.put("actualDriverUuid", uuid);
        maps.put("payStatus", "0");
        List<OrderDto> list = orderService.list(maps).getData();

        if (list.size() > 0) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "你有乘客未支付的订单，不能注销");
        }
        maps.put("payStatus", "2");
        List<OrderDto> list1 = orderService.list(maps).getData();
        if (list1.size() > 0) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "你有乘客正在退款中的订单，不能注销");
        }
        //获取当前司机是否有未处理或处理中的投诉信息
        Map<String, Object> map1 = MapUtils.build();
        map1.put("respondentIdentity", 1);
        map1.put("status", "1");
        map1.put("complainantUuid", uuid);
        List<OrderComplainDto> complainDtosList = orderComplainService.list(map1).getData();
        map1.put("status", "2");
        List<OrderComplainDto> complainDtosList1 = orderComplainService.list(map1).getData();
        if (complainDtosList1.size() > 0 || complainDtosList.size() > 0) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "你有未处理的投诉订单，不能注销");
        }
        //获取当前司机是否有未处理或处理中的被投诉信息
        Map<String, Object> map2 = MapUtils.build();
        map2.put("respondentIdentity", 2);
        map2.put("status", "1");
        map2.put("respondentUuid", uuid);
        List<OrderComplainDto> complainDtosList2 = orderComplainService.list(map2).getData();
        map2.put("status", "2");
        List<OrderComplainDto> complainDtosList3 = orderComplainService.list(map2).getData();
        if (complainDtosList2.size() > 0 || complainDtosList3.size() > 0) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "你有未处理的投诉订单，不能注销");
        }
        //执行注销删除司机操作
        DriverDto driverDto = new DriverDto();
        driverDto.setUuid(uuid);
        driverService.del(driverDto);
        //清空redis缓存
        String jsonTokenMap = redisStringCacheService.get(DRIVER_UUID + uuid, false);
        if (StringUtils.isEmpty(jsonTokenMap)) {
            return ResponseData.buildSuccessResponse("注销成功", true);
        }
        redisStringCacheService.del(DRIVER_UUID + uuid, false);
        return ResponseData.buildSuccessResponse("注销成功！", true);
    }

}
